<?php

namespace App\Listeners;

use App\Events\NewUserEvent;
use App\Models\User;
use App\Models\UserInvite;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class InviteEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(NewUserEvent $event)
    {
        if ($event->user->parent_id) {
            $model = new UserInvite();
            $model->user_id = $event->user->parent_id;
            $model->child_user_id = $event->user->id;
            $model->save();

            User::where('id', $event->user->parent_id)->increment('invite_count');
        }
    }
}
